package com.botarena.server.services;

import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;

import com.botarena.server.dao.AccountDAO;
import com.botarena.server.dao.helper.NonTransactionalReturn;
import com.botarena.server.dao.helper.TransactionalReturn;
import com.botarena.server.model.Account;
import com.botarena.server.services.validation.AccountValidator;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class AccountService extends GenericService<Account, AccountDAO, AccountValidator> {
	
	private static final UserService userService = UserServiceFactory.getUserService();
	
	public static final int MAX_NUMBER_OF_BOTS = 5;
	
	
	public boolean isLoggedIn() {
		return getGoogleUser() != null;
	}
	
	public User getGoogleUser() {
		return userService.getCurrentUser();
	}
	
	public boolean isAdmin() {
		return userService.isUserAdmin();
	}
	
	public String getLoginURL(String requestUri) {
		return userService.createLoginURL(requestUri);
	}
	
	public String getLogoutURL(String requestUri) {
		return userService.createLogoutURL(requestUri);
	}
	
	public String getAvatarURL(Account account) {
		if (account.getAvatar() != null)
			return "/servlet/http/images?avatar=" + KeyFactory.keyToString(account.getKey());
		return null;
	}
	
	public Account getAccount(final User gUser) {
		return DAOService.execute(new NonTransactionalReturn<Account>() {

			@Override
			public Account doOutsideTransaction(PersistenceManager pm) {
				try {
					AccountDAO accountDAO = new AccountDAO(pm);
					Key key = accountDAO.createKey(gUser.getEmail());
					return accountDAO.getByKey(key);
				} catch (JDOObjectNotFoundException e) {
					return null;
				}
			}
		});
	}
	
	public Account createAccount(final User gUser, final String name, final Blob avatar) {
		return DAOService.execute(new NonTransactionalReturn<Account>() {
			
			@Override
			public Account doOutsideTransaction(PersistenceManager pm) {
				AccountDAO accountDAO = new AccountDAO(pm);
				
				Account account = new Account(gUser.getEmail(), gUser.getNickname(), name);
				account.setKey(accountDAO.createKey(account));
				account.setAvatar(avatar);
				
				accountDAO.create(account);
				return account;
			}
		});
	}
	
	public Account updateAccount(final Key key, final String name, final Blob avatar, final boolean deleteAvatar) {
		return DAOService.execute(new TransactionalReturn<Account>() {
			
			@Override
			public Account doInTransaction(PersistenceManager pm) {
				AccountDAO accountDAO = new AccountDAO(pm);
				
				Account account = accountDAO.getByKey(key);
				account.setName(name);
				if (avatar != null)
					account.setAvatar(avatar);	
				if (deleteAvatar)
					account.setAvatar(null);
				
				accountDAO.create(account);
				return account;
			}
		});
	}

}
